package movie.controller;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alipay.api.AlipayApiException;
import movie.entity.AlipayBean;
import movie.entity.Cinema;
import movie.entity.Consumer;
import movie.entity.OrderSeat;
import movie.service.CinemaScheduleFindAllService;
import movie.service.OrderService;
import movie.service.PayService;
import movie.service.SeatService;

/**
 * 支付接口
 * @author CQUDJ
 *
 */
@Controller()
public class PayController {

	@Autowired
	private PayService payService;
	@Autowired
	private SeatService seatService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private CinemaScheduleFindAllService cinemaScheduleFindAllService;
	@RequestMapping(value = "/buyClass", method=RequestMethod.POST)
	public @ResponseBody String get(int recharge_type) throws UnsupportedEncodingException, AlipayApiException {
		AlipayBean alipayBean = new AlipayBean();
		int totalPrice=recharge_type*5-5;
		String WIDtotal_amount=Integer.toString(totalPrice);
		String WIDout_trade_no=getOrder();
		String WIDsubject="会员充值";
		String WIDbody="充值一个月会员";
		alipayBean.setOut_trade_no(WIDout_trade_no);
		alipayBean.setSubject(WIDsubject);
		alipayBean.setTotal_amount(WIDtotal_amount);
		alipayBean.setBody(WIDbody);
		String result=payService.aliPay(alipayBean);
		return result;
	}
	
	@RequestMapping(value = "/buy", method=RequestMethod.GET)
	public String get() {
		return "/buy/alipay";
	}
	@RequestMapping(value = "/buy", method=RequestMethod.POST)
	public @ResponseBody String post(String scheduleId,String[] seats,HttpServletRequest request) throws AlipayApiException, UnsupportedEncodingException {
		//String MovieName, String CinemaName, String HallName, String Version,String Time,String Price,
		System.out.println(scheduleId);
		HttpSession session = request.getSession();
        session.setAttribute("scheduleId",scheduleId);
        session.setAttribute("seats", seats);
		int L=seats.length;
		int id = Integer.parseInt(scheduleId);
		//scheduleId-排片信息
		Cinema cinemaMassage=cinemaScheduleFindAllService.findByScheduleId(id);
		String WIDout_trade_no=getOrder();
		String WIDsubject="在线购票";
		float discount=1.0f;
		Consumer consumer = (Consumer)session.getAttribute("cur_user");
		discount=getDiscount(consumer);
		float totalPrice=cinemaMassage.getPrice()*L*discount;
		String WIDtotal_amount=Float.toString(totalPrice);
		String WIDbody=cinemaMassage.getCinemaName().getCinemaName()+"-"+"-"+cinemaMassage.getHall().getHallName()+"-"+cinemaMassage.getFilm().getFilmName()
				+"-"+cinemaMassage.getFilmVersion()+"-"+cinemaMassage.getFilmBeginTimeString();		
		AlipayBean alipayBean = new AlipayBean();
		alipayBean.setOut_trade_no(WIDout_trade_no);
		alipayBean.setSubject(WIDsubject);
		alipayBean.setTotal_amount(WIDtotal_amount);
		alipayBean.setBody(WIDbody);
		String result=payService.aliPay(alipayBean);
		//OrderSeat orderSeat=new OrderSeat();
		
		//设置座位状态
		for(String i:seats) {
			int seatId=Integer.parseInt(i);
			//System.out.println(seatService.setSeatState(seatId));
		}
		return result;
	}
	public String getOrder() {

		Calendar cal = Calendar.getInstance();
		
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1; // 0代表1月，11代表12月
		int day = cal.get(Calendar.DAY_OF_MONTH);
		int hour = cal.get(Calendar.HOUR);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);
		String Order=year+month+day+hour+minute+second+"";
		return Order;
	}
	public float getDiscount(Consumer consumer) {
		if(consumer.getMemberId()==1) {
			return 0.99f;
		}
		else if(consumer.getMemberId()==2){
			return 0.95f;
		}
		else if(consumer.getMemberId()==3) {
			return 0.9f;
		}
		else {
			return 0.85f;
		}
	}
}

